package com.a2j.web.car.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.a2j.beans.base.BaseBean;
import com.a2j.beans.base.BasePageBean;
import com.a2j.beans.base.code.ResponseCode;
import com.a2j.beans.car.series.*;
import com.a2j.exception.BizException;
import com.a2j.utils.helper.PageHelper;
import com.a2j.web.car.mapper.CarSeriesMapper;
import com.a2j.web.car.service.CarSeriesService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

/**
 * Author: bzb
 * Data: 2022/1/14 22:25
 * Desc: 车系service实现
 */
@Service
public class CarSeriesImpl implements CarSeriesService {

    @Resource
    CarSeriesMapper mapper;

    @Override
    public void addCarSeries(CarSeriesDTO record) {
        Assert.notNull(record, "车系对象为空!");
        Assert.notNull(record.getSubBrandId(), "子品牌id不能为空!");
        Assert.hasLength(record.getName(), "车系名称为空!");

        CarSeriesPO po = CarSeriesPO.builder()
                .subBrandId(record.getSubBrandId())
                .name(record.getName())
                .remark(record.getRemark())
                .build();
        int result = mapper.addCarSeries(po);
        if (result == 0) throw new BizException(ResponseCode.FAILED_ADD);
    }

    @Override
    public void delCarSeries(Integer id) {
        Assert.notNull(id, "车系id为空!");

        int result = mapper.delCarSeries(id);
        if (result == 0) throw new BizException(ResponseCode.FAILED_DELETE);
    }

    @Override
    public void enable(Integer id, Integer enable) {
        Assert.notNull(id, "车系id为空!");
        if (enable == null) enable = 0;

        CarSeriesPO po = CarSeriesPO.builder()
                .id(id)
                .enable(enable)
                .build();
        int result = mapper.updateInfo(po);
        if (result == 0) throw new BizException(ResponseCode.FAILED_UPDATE);
    }

    @Override
    public void updateInfo(CarSeriesVO record) {
        Assert.notNull(record, "车系对象为空!");
        Assert.notNull(record.getSeriesId(), "车系id为空!");

        CarSeriesPO po = CarSeriesPO.builder()
                .id(record.getSeriesId())
                .name(record.getSeriesName())
                .subBrandId(record.getSubBrandId())
                .remark(record.getRemark())
                .enable(record.getEnable())
                .build();
        int result = mapper.updateInfo(po);
        if (result == 0) throw new BizException(ResponseCode.FAILED_UPDATE);
    }

    @Override
    public BaseBean<CarSeriesVO> getSeriesDetail(Integer id) {
        Assert.notNull(id, "车系id为空!");

        CarSeriesBO result = mapper.getSeriesDetail(id);
        if (result == null) throw new BizException(ResponseCode.NO_DATA);

        CarSeriesVO vo = CarSeriesVO.builder().build();
        BeanUtil.copyProperties(result, vo);

        return BaseBean.success(vo);
    }

    @Override
    public BaseBean<BasePageBean<CarSeriesVO>> getCarSeriesList(CarSeriesPagesBO record) {
        Assert.notNull(record, "车系对象为空!");

        PageHelper.checkPage(record);

        CarSeriesPagesBO bo = CarSeriesPagesBO.builder()
                .brandName(record.getBrandName())
                .seriesName(record.getSeriesName())
                .createTime(record.getCreateTime())
                .enable(record.getEnable())
                .page(record.getPage())
                .rows(record.getRows())
                .build();
        List<CarSeriesBO> result = mapper.getCarSeriesList(bo);
        if (result == null) throw new BizException(ResponseCode.NO_DATA);

        List<CarSeriesVO> vo = BeanUtil.copyToList(result, CarSeriesVO.class);
        if (vo == null) throw new BizException(ResponseCode.NO_DATA);

        int total = mapper.selectCount(bo);

        return new PageHelper<CarSeriesVO>().transformData(record.getPage(), record.getRows(), total, vo);
    }
}
